<template>
	<view class="uni-common-pb uni-padding-wrap uni-common-mt">
		<view class="uni-form-item uni-column">
			<view class="title">账户</view>
			<input class="uni-input" v-model="username" placeholder="请输入账户名" />
		</view>
		<view class="uni-form-item uni-column">
			<view class="title">密码</view>
			<input class="uni-input" password type="text" v-model="password" placeholder="请输入密码" />
		</view>

		<button type="primary" @tap="register">注册</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				username: '',
				password: '',
				autologin: true,
				loading: false
			};
		},
		onLoad(e) {

		},
		methods: {
			register2() {
				if (this.username == "") {
					uni.showToast({
						title: '请输入用户名！',
						icon: 'none',
						mask: true,
						duration: 3000
					});
					return;
				}

				if (this.password.length < 1) {
					uni.showToast({
						title: '请输入密码！',
						icon: 'none',
						mask: true,
						duration: 3000
					});
					return;
				}

				this.username = this.username.toLowerCase();
				this.password = this.password.toLowerCase();

				uni.setStorage({
					key: 'username',
					data: this.username,
					success: function() {
						console.log('success');
					}
				});

				// uni.setStorage({
				// 	key: 'password',
				// 	data: this.password,
				// 	success: function() {
				// 		console.log('success');
				// 	}
				// });

				uni.setStorage({
					key: this.username + '-password',
					data: this.password,
					success: function() {
						console.log('success');
					}
				});

				uni.showToast({
					title: '注册成功！',
					icon: 'none',
					mask: true,
					duration: 3000
				});
				uni.navigateBack();
				return;
			},
			register() {
				if (this.loading) {
					return;
				}
				this.loading = true;
				uni.showLoading({
					title: '登录中...'
				});

				console.log(this.url + '/api/feige/register?username=' + this.username + "&password=" + this.password);

				uni.request({
					url: this.url + '/api/feige/register',
					data: {
						"username": this.username,
						"password": this.password,
					},
					success: (res) => {
						if (res.data.code == 1) {
							uni.setStorage({
								key: 'username',
								data: this.username,
								success: function() {
									console.log('success');
								}
							});

							uni.showToast({
								title: '注册成功！',
								icon: 'none',
								mask: true,
								duration: 3000
							});
							uni.navigateBack();
							return;
						} else {
							uni.showToast({
								title: res.data.msg,
								icon: 'none',
								mask: true,
								duration: 3000
							});
						}
					},
					fail: (err) => {
						uni.showToast({
							title: err,
							icon: 'none',
							mask: true,
							duration: 3000
						});
					},
					complete: () => {
						uni.hideLoading();
						this.loading = false;
					}
				});

			}
		}
	}
</script>

<style>
	button {
		margin-top: 30upx;
		margin-bottom: 30upx;
	}
</style>
